<?php

class User extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public static function loadUser($name)
	{
		$user=self::loadUserFromCache($name);
		if($user!==null)
			return $user;
		$user=self::loadUserFromDb($name);
		if($user!==null)
			self::saveUserToCache($user);
		return $user;
	}

	public static function saveUser(&$user)
	{
		$ret=self::saveUserToDb($user);
		if($ret)
			self::saveUserToCache($user);
		return $ret;	
	}
	
	public static function deleteUser(&$user)
	{
		$ret=self::deleteUserFromDb($user);
		if($ret)
			self::deleteUserFromCache($user);
		return $ret;
	}

	public static function loadUserFromCache($name)
	{
		//TODO:
		return null;
	}

	public static function saveUserToCache(&$user)
	{
		//TODO:
		return true;
	}
	
	public static function deleteUserFromCache(&$user)
	{
		//TODO:
		return true;
	}

	public static function loadUserFromDb($name)
	{
		return self::model()->find('name=:name', array(':name'=>$name));
	}

	public static function saveUserToDb(&$user)
	{
		return $user->save();
	}
	
	public static function deleteFromDb(&$user)
	{
		return $user->delete();
	}

	public function tableName()
	{
		return 'User';
	}

	public function rules()
	{
		return array(
			array('name','length','max'=>16),
			array('password','length','max'=>16),
			array('name, password', 'required','on'=>'register'),
			array('petNum, activePet, location, win, lose, reputation, money', 'numerical', 'integerOnly'=>true),
		);
	}

	public function attributeLabels()
	{
		return array(
		);
	}

	public function addPet(&$pet,$setActive=false)
	{
		if($setActive)
			$oldActivePet=$this->activePet;

		++$this->petNum;
		$pet->userName=$this->name;
		$transaction=Yii::app()->db->beginTransaction();
		try{
			Pet::savePetToDb($pet);
			if($setActive){
				$this->activePet=$pet->id;
			}
			self::saveUserToDb($this);
			$transaction->commit();
		}
		catch(Exception $e){
			$transaction->rollBack();
			unset($pet->id);
			--$this->petNum;
			if($setActive)
				$this->activePet=$oldActivePet;
			throw $e;
		}

		self::saveUserToCache($this);
		Pet::savePetToCache($pet);			
	}

	public function deletePet(&$pet)
	{
		$active=$pet->id==$this->activePet;
		if($active){
			$criteria=new CDbCriteria;
			$criteria->select='id';
			$criteria->condition='userName=:userName';
			$criteria->params=array(':userName'=>$this->name);
			$criteria->limit=1;
		}
		--$this->petNum;

		$transaction=Yii::app()->db->beginTransaction();
		try{
			Pet::deletePetFromDb($pet);
			if($active){
				$alivePets=Pet::ListPet($criteria);
				if(empty($alivePets))
					throw new CException('no alive pets');
				$this->activePet=$alivePets[0]->id;
			}

			self::saveUserToDb($this);
			$transaction->commit();
		}
		catch(Exception $e){
			$transaction->rollBack();
			++$this->petNum;
			if($active)
				$this->activePet=$pet->id;
			throw $e;
		}

		self::saveUserToCache($this);
		Pet::deletePetFromCache($pet);			
	}

	public function activatePet($pet)
	{
		$this->activePet=$pet->id;
		self::saveUser($this);
	}

	protected function beforeSave()
	{
		if($this->isNewRecord)
			$this->createDt=new CDbExpression('NOW()');
		return true;
	}
}
